package Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @author 李某
 * * @date 2021/11/19
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */


public class ServletFrom {}

// 使用 Get 方法示例

    /**
     * Servlet implementation class HelloForm
     * 服务程序      执行         类   HelloForm
     */
//    @WebServlet("/HelloForm")
    // 网络服务程序  （网址访问 /HelloForm）

    class HelloForm extends HttpServlet {
        // 公开 类 HelloForm 继承 服务端
        private static final long serialVersionUID = 1L;
        // 私有   静态   终极 long类型  序列版本UID = 1

        /**
         * @see HttpServlet#HttpServlet()
         */
        public HelloForm() {
            // 构造方法
            super();
            // 调用父类
            // TODO Auto-generated constructor stub
            // 自动     自动生成       构造函数    存根
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 受保护的  doGet方法     Http请求      要求        Http响应回复        回复      抛出     Servlet异常       IO异常
            // 设置响应内容类型
            response.setContentType("text/html;charset=UTF-8");
            // 回复    回复内容格式    HTML       字符集  = UTF-8

            PrintWriter out = response.getWriter();
            // 排版输出  输出 =    回复     获得输出
            String title = "使用 GET 方法读取表单数据";

            // 处理中文
            String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
            // 字符串 名字 = 新的 字符串（要求 获得参数（name）. 获取字节 （charsetName：字符集名称：ISO-8859-1 ），字符集名称 “UTF-8” ）

            // 至此一下，输出的为 HTMl 拼接类型
            String docType = "<!DOCTYPE html> \n";

            out.println(docType +
                    "<html>\n" +
                    "<head><title>" + title + "</title></head>\n" +
                    "<body bgcolor=\"#f0f0f0\">\n" +
                    "<h1 align=\"center\">" + title + "</h1>\n" +
                    "<ul>\n" +
                    "  <li><b>站点名</b>："
                    + name + "\n" +
                    "  <li><b>网址</b>："
                    + request.getParameter("url") + "\n" +
                    "</ul>\n" +
                    "</body></html>");
        }

        // 处理 POST 方法请求的方法
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
//现在在浏览器的地址栏中输入
// ***http://localhost:8080/TomcatTest/HelloForm
// 使用表单的GET方法示例
/**
 * <!DOCTYPE html>
 * <html>
 * <head>
 * <meta charset="utf-8">
 * <title>Test</title>
 * </head>
 * <body>
 * <form action="HelloForm" method="GET">
 * 网址名：<input type="text" name="name">
 * <br />
 * 网址：<input type="text" name="url" />
 * <input type="submit" value="提交" />
 * </form>
 * </body>
 * </html>
 */




// 使用表单的 POST 方法
/**
 * Servlet implementation class HelloForm
 */
//@WebServlet("/HelloForm")
class HelloForm1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloForm1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String title = "使用 POST 方法读取表单数据";
        // 处理中文
        String name = new String(request.getParameter("name").getBytes("ISO8859-1"), "UTF-8");
        String docType = "<!DOCTYPE html> \n";
        out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>站点名</b>："
                + name + "\n" +
                "  <li><b>网址</b>："
                + request.getParameter("url") + "\n" +
                "</ul>\n" +
                "</body></html>");
    }

    // 处理 POST 方法请求的方法
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
/**
 * 本质调用的 还是 doGet  方法
 * String name = new String
 * (request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
 * <p>
 * <!DOCTYPE html>
 * <html>
 * <head>
 * <meta charset="utf-8">
 * <title>菜鸟教程(runoob.com)</title>
 * </head>
 * <body>
 * <form action="HelloForm" method="POST">
 * 网址名：<input type="text" name="name">
 * <br />
 * 网址：<input type="text" name="url" />
 * <input type="submit" value="提交" />
 * </form>
 * </body>
 * </html>
 */  // post 方法文本框HTML




// 使用 Post 方法 提交的 复选框 选择
/**
 * Servlet implementation class CheckBox
 */
//@WebServlet("/CheckBox")
class CheckBox extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String title = "读取复选框数据";
        String docType = "<!DOCTYPE html> \n";
        out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>菜鸟按教程标识：</b>: "
                + request.getParameter("runoob") + "\n" +
                "  <li><b>Google 标识：</b>: "
                + request.getParameter("google") + "\n" +
                "  <li><b>淘宝标识：</b>: "
                + request.getParameter("taobao") + "\n" +
                "</ul>\n" +
                "</body></html>");
    }

    // 处理 POST 方法请求的方法
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

// Post 方法 提交的复选框 HTML代码
/**
 * <!DOCTYPE html>
 * <html>
 * <head>
 * <meta charset="utf-8">
 * <title>菜鸟教程(runoob.com)</title>
 * </head>
 * <body>
 * <form action="CheckBox" method="POST" target="_blank">
 * <input type="checkbox" name="runoob" checked="checked" /> 菜鸟教程
 * <input type="checkbox" name="google"  /> Google
 * <input type="checkbox" name="taobao" checked="checked" /> 淘宝
 * <input type="submit" value="选择站点" />
 * </form>
 * </body>
 * </html>
 * <p>
 * <?xml version="1.0" encoding="UTF-8"?>
 * <web-app>
 * <servlet>
 * <servlet-name>CheckBox</servlet-name>
 * <servlet-class>com.runoob.test.CheckBox</servlet-class>
 * </servlet>
 * <servlet-mapping>
 * <servlet-name>CheckBox</servlet-name>
 * <url-pattern>/TomcatTest/CheckBox</url-pattern>
 * </servlet-mapping>
 * </web-app>
 */
// Post 方法 提交的复选框 XMl 代码
/**
 * <?xml version="1.0" encoding="UTF-8"?>
 * <web-app>
 *   <servlet>
 *     <servlet-name>CheckBox</servlet-name>
 *     <servlet-class>com.runoob.test.CheckBox</servlet-class>
 *   </servlet>
 *   <servlet-mapping>
 *     <servlet-name>CheckBox</servlet-name>
 *     <url-pattern>/TomcatTest/CheckBox</url-pattern>
 *   </servlet-mapping>
 * </web-app>
 */




// 读取所有的 表单参数
/*
以下是通用的实例，使用 HttpServletRequest 的 getParameterNames() 方法读取所有可用的表单参
    数。该方法返回一个枚举，其中包含未指定顺序的参数名。

一旦我们有一个枚举，我们可以以标准方式循环枚举，使用 hasMoreElements() 方法来确定何时停止，
    使用 nextElement() 方法来获取每个参数的名称。
 */
/**
 * Servlet implementation class ReadParams
 *  服务        执行         类    读取参数
 */
//@WebServlet("/ReadParams")
// wed服务     地址/ReadParams
class ReadParams extends HttpServlet {
    // 类 读取参数  继承  Http服务
    private static final long serialVersionUID = 1L;
    // 私有的 静态的 最后的  long类型  串行版本UID = 1
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReadParams() {
        // 参数读取
        super(); // 调用父类的 无参构造
        // TODO Auto-generated constructor stub
        // 自动生成                构造函数   存根
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String title = "读取所有的表单数据";
        String docType =
                "<!doctype html public \"-//w3c//dtd html 4.0 " +
                        "transitional//en\">\n";
        out.println(docType +
                "<html>\n" +
                "<head><meta charset=\"utf-8\"><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<table width=\"100%\" border=\"1\" align=\"center\">\n" +
                "<tr bgcolor=\"#949494\">\n" +
                "<th>参数名称</th><th>参数值</th>\n" +
                "</tr>\n");

        Enumeration paramNames = request.getParameterNames();
        //   枚举     参数名称       要求 .   获取参数名称

        while (paramNames.hasMoreElements()) {
            // 循环  参数名称  获取更多的参数
            String paramName = (String) paramNames.nextElement();
            out.print("<tr><td>" + paramName + "</td>\n");
            String[] paramValues =
                    request.getParameterValues(paramName);
            // 读取单个值的数据
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                    out.println("<td><i>没有值</i></td>");
                else
                    out.println("<td>" + paramValue + "</td>");
            } else {
                // 读取多个值的数据
                out.println("<td><ul>");
                for (int i = 0; i < paramValues.length; i++) {
                    out.println("<li>" + paramValues[i]);
                }
                out.println("</ul></td>");
            }
            out.print("</tr>");
        }
        out.println("\n</table>\n</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }


}
// 配套 Html
/**
 * <!DOCTYPE html>
 * <html>
 * <head>
 * <meta charset="utf-8">
 * <title>TEst</title>
 * </head>
 * <body>
 *
 * <form action="ReadParams" method="POST" target="_blank">
 * <input type="checkbox" name="maths" checked="checked" /> 数学
 * <input type="checkbox" name="physics"  /> 物理
 * <input type="checkbox" name="chemistry" checked="checked" /> 化学
 * <input type="submit" value="选择学科" />
 * </form>
 *
 * </body>
 * </html>
 */
// 配套 Xml
/**
 * <?xml version="1.0" encoding="UTF-8"?>
 * <web-app>
 *   <servlet>
 *     <servlet-name>ReadParams</servlet-name>
 *     <servlet-class>com.runoob.test.ReadParams</servlet-class>
 *   </servlet>
 *   <servlet-mapping>
 *     <servlet-name>ReadParams</servlet-name>
 *     <url-pattern>/TomcatTest/ReadParams</url-pattern>
 *   </servlet-mapping>
 * </web-app>
 */












